package com.example.pkmedical.Activity.weight.tozero;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.Base.BaseFragment;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.DistinguishUtil;
import com.example.pkmedical.Utils.SPUtils;
import com.example.pkmedical.Utils.SerialProtUtil;

import java.text.DecimalFormat;
import java.util.Arrays;

public class ReleaseZeroNextActivity extends BaseActivity {
    private static final String TAG = "yangshuo00";
    byte[] mBytes;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_releasezero_next);
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void initData() {
        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.CALIBRATION_TOZERO_TWO);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        if (event.getCode() == MessageWrap.WEIGHT_ZERO) {
            Log.d(TAG, "onDataReceivedToZeroTwo [ byte[] ]: " + Arrays.toString(event.getBytes()));
            mBytes = event.getBytes();
            distinguish(mBytes);
        } else {
            Log.d(TAG, "onDataReceivedToZeroTwo [ byte[] ]: NoThisData");
        }
    }

    /**
     * 返回数据识别
     */
    private void distinguish(byte[] bytes) {
        if (DistinguishUtil.distinguishUtil(bytes)) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    switch (bytes[3]) {
                        case 0x01:
                            doNext();
                            break;
                    }
                }
            });
        } else {
        }
    }

    private void doNext() {
        DecimalFormat df = new DecimalFormat("#.00");
        String nowWeight = SPUtils.getInstance().getString("user_now_weight");
        String allThing = SPUtils.getInstance().getString("user_difference_weight_all");
        if (!"不适用".equals(nowWeight)){
            Float db = Float.parseFloat(nowWeight)-Float.parseFloat(allThing);
            SPUtils.getInstance().put("user_now_weight",df.format(db)+"");
        }
        SPUtils.getInstance().put("user_difference_weight_all", "0");
        startActivity(new Intent(MyApp.getAppContext(), ToZeroSuccessActivity.class));
        ReleaseZeroNextActivity.this.finish();
    }

    @Override
    public void showDialog(String... msg) {

    }
}
